package com.lin.dao.user;

import com.lin.dao.BaseDao;
import com.lin.pojo.User;
import com.mysql.cj.util.StringUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao{
    public User getUserInfo(Connection connection,String userCode) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        User user = null;
        if (null != connection){
            String sql = "select * from smbms_user where userCode=?";
            Object[] params = {userCode};
            rs = BaseDao.execute(connection, preparedStatement, rs, sql, params);
            if (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUserCode(rs.getString("userCode"));
                user.setUserName(rs.getString("userName"));
                user.setUserPassword(rs.getString("userPassword"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setUserRole(rs.getInt("userRole"));
                user.setCreatedBy(rs.getInt("createdBy"));
                user.setCreationDate(rs.getTimestamp("creationDate"));
                user.setModifyBy(rs.getInt("modifyBy"));
                user.setModifyDate(rs.getTimestamp("modifyDate"));
            }
            BaseDao.closeResource(connection,preparedStatement,rs);
        }

        return user;
    }


    @Test
    public static void main(String[] args) {
        UserDaoImpl dao = new UserDaoImpl();
        Connection connect = BaseDao.getConnect();
        try {
//            List<User> userList = dao.getUserList(connect, null, 0, 1, 10);
//            for (User user : userList) {
//                System.out.println(user);
//            }
//            int count = dao.getUserCount(connect, "", 0);
//            System.out.println(count);
            User userInfoById = dao.getUserInfoById(connect, 1);
            System.out.println(userInfoById);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }
    public boolean modifyPassword(Connection connection,Integer id, String userPassword) throws Exception {
        PreparedStatement preparedStatement = null;
        boolean flag = false;
        if (null != connection){
            String sql = "UPDATE `smbms_user` SET userPassword=? WHERE id=?";
            Object[] params= {userPassword,id};
            int i = BaseDao.execute(connection, preparedStatement, sql, params);
            if (i>0){
                flag = true;
            }
        }
        return flag;
    }

    public List<User> getUserList(Connection connection, String userName, Integer userRole, Integer pageIndex, Integer pageSize) throws Exception {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<User> users = new ArrayList<User>();
        if (connection != null){
            StringBuffer sql = new StringBuffer();
            sql.append("select u.*,r.roleName as userRoleName from smbms_user u left join smbms_role r on u.userRole=r.id where 1=1 ");
            List<Object> list = new ArrayList<Object>();

            if (!StringUtils.isNullOrEmpty(userName)){
                sql.append(" and u.userName like ?");
                list.add("%"+userName+"%");
            }
            if (userRole >0){
                sql.append(" and u.userRole = ?");
                list.add(userRole);
            }

            sql.append(" order by creationDate desc limit ?,? ");

            pageIndex = (pageIndex-1)*pageSize;
            list.add(pageIndex);
            list.add(pageSize);

            resultSet = BaseDao.execute(connection, preparedStatement, resultSet, sql.toString(), list.toArray());
            while (resultSet.next()){
                User _user = new User();
                _user.setId(resultSet.getInt("id"));
                _user.setUserCode(resultSet.getString("userCode"));
                _user.setUserName(resultSet.getString("userName"));
                _user.setGender(resultSet.getInt("gender"));
                _user.setBirthday(resultSet.getDate("birthday"));
                _user.setPhone(resultSet.getString("phone"));
                _user.setUserRole(resultSet.getInt("userRole"));
                _user.setUserRoleName(resultSet.getString("userRoleName"));
                users.add(_user);
            }
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return users;
    }

    public Integer getUserCount(Connection connection, String userName, Integer userRole) throws Exception {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int count = 0;
        if (null != connection){
            StringBuffer sql = new StringBuffer();
            ArrayList<Object> list = new ArrayList<Object>();
            sql.append("select count(1) as count from smbms_user u left join smbms_role r on u.userRole=r.id where 1=1 ");
            if (!StringUtils.isNullOrEmpty(userName)){
                sql.append(" and u.userName like ?");
                list.add("%"+userName+"%");
            }
            if (userRole >0){
                sql.append(" and u.userRole = ?");
                list.add(userRole);
            }
            System.out.println("sql");
            System.out.println(sql);
            System.out.println("list");
            System.out.println(list);

            Object[] params = list.toArray();
            resultSet = BaseDao.execute(connection, preparedStatement, resultSet, sql.toString(), params);
           if (resultSet.next()){
                count = resultSet.getInt("count");
           }

           BaseDao.closeResource(null,preparedStatement,resultSet);
        }

        return count;
    }

    public User getUserInfoById(Connection connection, Integer id) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        User user = null;
        if (connection != null){
            String sql = "select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.id=? and u.userRole = r.id";
            Object[] params = {id};
            rs = BaseDao.execute(connection, preparedStatement, rs, sql, params);
            if (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUserCode(rs.getString("userCode"));
                user.setUserName(rs.getString("userName"));
                user.setUserPassword(rs.getString("userPassword"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setUserRole(rs.getInt("userRole"));
                user.setCreatedBy(rs.getInt("createdBy"));
                user.setCreationDate(rs.getTimestamp("creationDate"));
                user.setModifyBy(rs.getInt("modifyBy"));
                user.setModifyDate(rs.getTimestamp("modifyDate"));
                user.setUserRoleName(rs.getString("userRoleName"));
            }
            BaseDao.closeResource(null,preparedStatement,rs);
        }
        return user;
    }

    public boolean modifyUserInfoById(Connection connection, Integer id, User user) throws Exception {
        PreparedStatement preparedStatement = null;
        boolean flag = false;
        if (connection != null){
            String sql = "update smbms_user set userName=?,gender=?,birthday=?,phone=?,address=?,userRole=?,modifyBy=?,modifyDate=? where id = ? ";
            Object[] params = {user.getUserName(),user.getGender(),user.getBirthday(),
                    user.getPhone(),user.getAddress(),user.getUserRole(),user.getModifyBy(),
                    user.getModifyDate(),user.getId()};
            int i = BaseDao.execute(connection, preparedStatement, sql, params);
            if (i>0){
                flag = true;
            }
        }
        return flag;
    }

    public boolean addUserInfo(Connection connection, User user) throws Exception {
        PreparedStatement preparedStatement = null;
        boolean flag = false;
        if (connection != null){
            String sql = "insert into smbms_user (userCode,userName,userPassword," +
                    "userRole,gender,birthday,phone,address,creationDate,createdBy) " +
                    "values(?,?,?,?,?,?,?,?,?,?)";
            Object[] params = {user.getUserCode(),user.getUserName(),user.getUserPassword(),
                    user.getUserRole(),user.getGender(),user.getBirthday(),
                    user.getPhone(),user.getAddress(),user.getCreationDate(),user.getCreatedBy()};
            int execute = BaseDao.execute(connection, preparedStatement, sql, params);
            if (execute >1){
                flag = true;
            }
            BaseDao.closeResource(null, preparedStatement, null);
        }
        return flag;
    }

    public boolean deleteUserInfo(Connection connection, Integer id) throws Exception {
        PreparedStatement pstm = null;
        boolean flag = false;
        if(null != connection){
            String sql = "delete from smbms_user where id=?";
            Object[] params = {id};
            int execute = BaseDao.execute(connection, pstm, sql, params);
            if (execute >0){
                flag = true;
            }
            BaseDao.closeResource(null, pstm, null);
        }
        return flag;
    }
}
